﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EncryptionLib;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;
using EncryptionLib.Users;
using System.Diagnostics;

namespace TotalCommander_v3
{
    public partial class frmNewUser : Form
    {
        public frmNewUser()
        {
            InitializeComponent();
        }

        private void frmNewUser_Load(object sender, EventArgs e)
        {

        }

        private void cbkShowPassphrase_CheckedChanged(object sender, EventArgs e)
        {
            tbPassphrase.PasswordChar = cbkShowPassphrase.Checked ? '\0' : '*';
        }

        private void tbAdd_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbEmail.Text) || String.IsNullOrEmpty(tbName.Text)
                || String.IsNullOrEmpty(dtBirthday.Value.ToString()) || String.IsNullOrEmpty(tbAddress.Text)
                || String.IsNullOrEmpty(tbPhone.Text) || String.IsNullOrEmpty(tbPassphrase.Text))
            {
                MessageBox.Show("Yout must supply all information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            UserData user = new UserData(tbEmail.Text, tbName.Text, dtBirthday.Value, 
                                         tbPhone.Text, tbAddress.Text,
                                         tbPassphrase.Text, int.Parse(keyLength.Value.ToString()));

            // encrpytion key len
            if (frmMain.m_userManager.GetUserByEmail(user.Email) == null)
            {
                Debug.Assert(frmMain.m_userManager.Register(user));

                frmMain.m_userManager.SaveData();

                MessageBox.Show(this, "Create Successful!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(this, "This user is existed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
       
    }
}
